//
//  C7Calculator.m
//  精通Objective-C
//
//  Created by 花强 on 2017/11/28.
//  Copyright © 2017年 花强. All rights reserved.
//

#import <objc/runtime.h>
#import "C7Calculator.h"

@implementation C7Calculator

-(NSNumber *)sumAddend1:(NSNumber *)adder1 addend2:(NSNumber *)adder2{
    NSLog(@"Invoking method on %@ object with selector %@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
    return [NSNumber numberWithInteger:([adder1 integerValue]+[adder2 integerValue])];
}
-(NSNumber *)sumAddend1:(NSNumber *)adder1 :(NSNumber *)adder2{//这个方法的第二个参数使用空参数
    NSLog(@"Invoking method on %@ object with selector %@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
    return [NSNumber numberWithInteger:([adder1 integerValue]+[adder2 integerValue])];
}

id absoluteValue(id self , SEL _cmd , id value){
    NSInteger intVal = [value integerValue];
    if (intVal < 0) {
        NSLog(@"para object class:%@\npara method SEL:%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
        return [NSNumber numberWithInteger:(intVal * -1)];
    }
    return value;
}
+(BOOL)resolveInstanceMethod:(SEL)sel{
    NSString * method = NSStringFromSelector(sel);
    if ([method hasPrefix:@"absoluteValue"]) {
        class_addMethod([self class],sel,(IMP)absoluteValue,"@@:@");
        NSLog(@"Dynamic added instance method %@ to class %@",method,NSStringFromClass([self class]));
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}

@end
